<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="车牌号">
          <a-input
            placeholder="请输入车牌号"
            :disabled="disabledEdit"
            v-decorator="['plateNo', {rules: [{ type: 'string',required: true, message: '请输入车牌号!' }],}]"/>
        </a-form-item>

        <a-form-item v-show="false" :labelCol="labelCol" :wrapperCol="wrapperCol" label="是否是白名单">
          <a-select
            :disabled="disabledEdit"
            v-decorator="['licenseType', {initialValue:1}]"
            placeholder="请选择是否是白名单"
            @change="licenseTypeChange">
            <a-select-option :value="1">白名单</a-select-option>
            <a-select-option :value="2">黑名单</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item v-show="false" :labelCol="labelCol" :wrapperCol="wrapperCol" label="是否启用">
          <a-select :disabled="disabledEdit" v-decorator="['status', {initialValue:'Y'}]">
            <a-select-option :value="'Y'">启用</a-select-option>
            <a-select-option :value="'N'">冻结</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="选择车辆组类型">
          <a-select
            :options="carTypes"
            :disabled="disabledEdit"
            v-decorator="['carTypeId', {initialValue:defaultCarTypeId}]"
            placeholder="请选择月租车类型">
          </a-select>
        </a-form-item>


        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          v-show="licenseType!==3"
          :disabled="disabledEdit"
          label="停车场位编号">
          <a-input
            placeholder="停车场位编号"
            :disabled="disabledEdit"
            v-decorator="['parkNumber', {rules:[{max:10,message:'停车场位编码最大长度为10'}]}]"/>
        </a-form-item>

        <a-form-item
          :labelCol=" {xs: {span: 24},sm: {span: 5},}"
          :wrapperCol="{xs: {span: 24}, sm: {span: 4},}"
          v-show="licenseType!==3"
          label="起始时间">
          <a-date-picker
            v-show="licenseType!==3"
            format="YYYY-MM-DD"
            :disabled="disabledStartTime"
            v-decorator="[ 'startTime', {rules: [{ required: licenseType!==3, message: '请选择起始时间!' }],}]"/>
        </a-form-item>

        <a-form-item
          :labelCol=" {xs: {span: 24},sm: {span: 5},}"
          :wrapperCol="{xs: {span: 24}, sm: {span: 4},}"
          v-show="licenseType!==3"
          label="过期时间">
          <a-date-picker
            v-show="licenseType!==3"
            format="YYYY-MM-DD"
            :disabled="disabled"
            v-decorator="[ 'endTime', {rules: [{ required: licenseType!==3, message: '请选择过期时间!' }],}]"/>
        </a-form-item>

        <a-form-item
          :labelCol="{
            xs: {span: 24},
            sm: {span: 5},
          }"
          :wrapperCol="{
            xs: {span: 24},
            sm: {span: 13},
          }"
          label="客户姓名">
          <a-input
            placeholder="请输入客户姓名"
            :disabled="disabledEdit"
            v-decorator="['userName', {rules: [{ type: 'string', required: true, message: '请输入客户姓名!' },{max:40,message: '客户姓名长度不要超过40个字符'}]}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="{
            xs: {span: 24},
            sm: {span: 5},
          }"
          :wrapperCol="{
            xs: {span: 24},
            sm: {span: 13},
          }"
          label="客户手机号码">
          <a-input
            placeholder="请输入客户手机号码"
            :disabled="disabledEdit"
            v-decorator="['mobile', {rules: [{required: true,message: '请输入客户手机号'},{ validator:this.validateMobile}],}]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="支付费用(元)">
          <a-input-number :min="0" :max="2100000000" v-decorator="[ 'payFee', {initialValue:0}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="家庭住址">
          <a-input
            placeholder="请输入家庭住址"
            :disabled="disabledEdit"
            v-decorator="['address', {rules:[{max:100,message:'家庭住址长度不要超过100个字符'}]}]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="车辆备注">
          <a-input
            placeholder="请输入车辆备注"
            :disabled="disabledEdit"
            v-decorator="['remark', {rules:[{max:100,message:'车辆备注长度不要超过100个字符'}]}]"/>
        </a-form-item>

        <a-form-item
          v-show="false"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="修改时间">
          <a-date-picker
            :disabled="disabledEdit"
            format="YYYY-MM-DD HH:mm:ss"
            v-decorator="[ 'updateTime', {}]"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import {httpAction} from '@/api/manage'
import pick from 'lodash.pick'
import moment from "moment"

export default {
  name: "ParkWhiteListModal",
  data() {
    return {
      title: "操作",
      visible: false,
      model: {},
      disabledEdit: false,
      disabled: false,
      disabledStartTime: false,
      labelCol: {
        xs: {span: 24},
        sm: {span: 5},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },
      licenseType: '',
      parkList: [],
      carTypes: [],
      parkCode: '',
      defaultCarTypeId: '',
      parkName: '',
      confirmLoading: false,
      form: this.$form.createForm(this),
      url: {
        add: "/parkCar/parkCarMonthly/add",
        edit: "/parkCar/parkCarMonthly/edit",
        queryParkList: "/parkCar/parkCarMonthly/queryParkList",
        parkCarTypeList: "/ConfigCarType/configCarType/parkCarTypeList"
      },
    }
  },
  mounted() {

  },
  methods: {
    add() {
      this.edit({});
    },
    edit(record, judge) {
      this.disabledStartTime = false;
      this.disabled = false;
      this.disabledEdit = false;
      if (judge === 'delay') {
        this.disabledStartTime = true;
        this.disabledEdit = true;
      }
      if (judge === 'edit') {
        this.disabledStartTime = true;
        this.disabled = true;
      }
      this.visible = true;
      this.confirmLoading = true
      this.form.resetFields();
      this.licenseTypeChange(record.licenseType)
      this.model = Object.assign({}, record);
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'plateNo', 'licenseType', 'carTypeId', 'userName', 'mobile', 'status', 'payFee', 'address', 'remark', 'parkNumber'))
        //时间格式化
        if (this.model.licenseType !== 3) {
          this.form.setFieldsValue({startTime: this.model.startTime ? moment(this.model.startTime) : null})
          this.form.setFieldsValue({endTime: this.model.endTime ? moment(this.model.endTime) : null})
        }
        this.form.setFieldsValue({updateTime: this.model.updateTime ? moment(this.model.updateTime) : null})
        this.confirmLoading = false
      });

    },
    close() {
      this.$emit('close');
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          let formData = Object.assign(this.model, values);
          //时间格式化
          if (formData.licenseType !== 3) {
            formData.startTime = formData.startTime ? formData.startTime.format('YYYY-MM-DD 00:00:00') : null;
            formData.endTime = formData.endTime ? formData.endTime.format('YYYY-MM-DD 00:00:00') : null;
            if (formData.startTime > formData.endTime) {
              this.$message.error("开始时间不能大于结束时间");
              return
            }
          } else {
            formData.startTime = ''
            formData.endTime = ''
            formData.parkNumber = null
          }
          formData.updateTime = formData.updateTime ? formData.updateTime.format('YYYY-MM-DD HH:mm:ss') : null;
          if (formData.parkNumber !== undefined && formData.parkNumber !== null) {
            formData.parkNumber = formData.parkNumber.trim()
          }
          if (formData.carTypeId == "") {
            this.$message.error("请添加月租车类型");
            return
          } else {
            this.confirmLoading = true
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
                that.close();
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              this.confirmLoading = false
            })
          }
        }
      })
    },
    handleCancel() {
      this.close()
    },
    validateMobile(rule, value, callback) {
      if (!value || new RegExp(/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/).test(value)) {
        callback();
      } else {
        callback("手机号码格式不正确!");
      }
    },
    licenseTypeChange(value) {
      this.licenseType = value
    }
  }
}
</script>

<style lang="less" scoped>

</style>
